<?php

namespace App\Http\Controllers\UserApi;

use App\Models\GoodsIntegralExpire;
use Illuminate\Database\Eloquent\Model;
use App\Models\Goods;
use App\Models\GoodsOrder;
use Illuminate\Http\Request;
use App\Services\Api\GoodsServices;
use App\Models\GoodsIntegralChangeRecord;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Log;
use App\Models\SystemImages;

class GoodsController extends BaseController
{
    public function goods_list(Request $request,Goods $goods,GoodsServices $goodsServices){
        $user_id = auth('api') -> user()->id;
        $goods = $goods
            ->with([
                'goods_order'=>function($q)use($user_id){
                    $q->where('user_id',$user_id);
                }
            ])
            ->where('is_opend',1)
            ->orderBy('created_at','desc')
            ->paginate(15)
            ->toArray();
        $goods['my_goods_integral'] = $goodsServices->user_goods_integral($user_id,date("Y-m-d H:i:s"));
        $goods['my_goods_total'] = GoodsOrder::where('user_id',$user_id)->count();
        return self::success($goods);
    }

    public function goods_info(Request $request,Goods $goods){
        $user_id = auth('api') -> user()->id;
        $goods = $goods
            ->where('id',$request->input('goods_id'))
            ->with([
                'goods_order'=>function($q)use($user_id){
                    $q->where('user_id',$user_id);
                }
            ])
            ->first();
        return self::success($goods);
    }

    public function my_order(Request $request,GoodsOrder $goodsOrder){
        $user_id = auth('api') -> user()->id;
        $goods = $goodsOrder
            //->where('id',$request->goods_id)
            ->where('user_id',$user_id)
            ->with([
                'goods'
            ])
            ->orderBy('created_at','desc')
            ->paginate(15);
        return self::success($goods);
    }

    public function buy_goods(Request $request,GoodsServices $goodsServices){
        $user_id = auth('api') -> user()->id;

        $goods_id = $request->input('goods_id');

        DB::beginTransaction();
        try{

            $user_goods_integral = $goodsServices->user_goods_integral($user_id);

            $goods_result = Goods::where('id',$goods_id)->select('goods_price', 'stock', 'is_opend')->lock(true)->first();

            $goods_price = intval($goods_result->goods_price);
            $stock = intval($goods_result->stock);

            if($goods_result->goods_price > $user_goods_integral)throw new \Exception('积分不足!');

            if($goods_result->is_opend == 2)throw new \Exception('商品已下架!');

            if($stock <= 0)throw new \Exception('库存不足!');

            $goodsServices->buy_goods($user_id, $request->input('goods_id'), $goods_price);

            $goods_name = $goods_result->goods_name??'';

            $reason = "{$goods_name}-積分兌換";

            GoodsServices::goods_record($goods_price, 2, $reason);

            DB::commit();

        }catch (\Exception $e) {

            //接收异常处理并回滚
            DB::rollBack();
            return self::fail($e->getMessage());

        }

        return self::success();
    }

    public function my_goods_integral_record(GoodsIntegralChangeRecord $goodsIntegralChangeRecord,GoodsServices $goodsServices){
        $data = $goodsIntegralChangeRecord->where('user_id',auth('api') -> user()->id)
            ->orderBy('created_at','desc')
            ->paginate(15)->toArray();

        return self::success($data);
    }

    public function my_goods_integral(GoodsServices $goodsServices){
        $goods['my_goods_integral'] = $goodsServices->user_goods_integral(auth('api') -> user()->id);
        $goods['expire_duration'] = GoodsIntegralExpire::value('expire_duration');
        return self::success($goods);
    }

    public function goods_integral_cove(SystemImages $systemImages){
        $url = $systemImages->where('type',3)->first();
        return self::success($url);
    }
}
